package edu.wy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.wy.common.result.Result;
import edu.wy.entity.Login;
import edu.wy.entity.Users;
import edu.wy.service.UserService;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author rjh
 * @date 2025/4/17 16:05
 * 用户控制类
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;



    /**
     * 根据ID获取用户信息
     * @param id 用户id
     */
    @ApiResponse(description = "根据ID获取用户信息")
    @GetMapping("/info/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        return Result.ok(userService.getById(id));
    }

    /**
     * 根据id删除用户
     * @param id 用户id
     */
    @ApiResponse(description = "根据id删除用户")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return Result.ok(userService.removeById(id));
    }

    /**
     * 获取用户列表信息
     * @param page 页码
     * @param size 每页显示数量
     */

    @ApiResponse(description = "获取用户列表信息")
    @GetMapping("/list/page/size")
    public Result list(@RequestParam("page") Long page,@RequestParam("size") Long size){
        Page<Users> pageInfo = userService.query().page(new Page<>(page, size));
        return Result.ok(pageInfo.getRecords());
    }

    /**
     * 根据用户id修改用户信息
     * @param user 前端传回的用户信息
     */
    @ApiResponse(description = "根据用户id修改用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody Users user) {
        return Result.ok(userService.updateById(user));
    }

    /**
     * 根据手机号进行注册
     */
    @ApiResponse(description = "根据手机号进行注册")
    @PostMapping("/register")
    public Result register(@RequestBody Login login) {
        return userService.addUserByPhone(login);
    }

}
